import { ACPublisher } from './ACPublisher';
import { ACSubscriber } from './ACSubscriber';
import { ACSubscriberHolderSet } from './ACSubscriberHolderSet';

export class ACSubscriberHolder {

  private publisher?: WeakRef<ACPublisher<any>>;
  private subscriber?: ACSubscriber<any>;

  constructor(publisher: ACPublisher<any>, subscriber: ACSubscriber<any>) {
    this.publisher = new WeakRef(publisher);
    this.subscriber = subscriber;
  }

  store(cancellableSet: ACSubscriberHolderSet): ACSubscriberHolder {
    cancellableSet.store(this);
    this.subscriber?.setHolderSet(cancellableSet);
    return this;
  }

  cancelSubscribe() {
    this.publisher?.deref()?.cancelSubscribe();
    this.publisher = undefined;
    this.subscriber = undefined;
  }
}
